package com.alks.function.controller.biocontrol.webWork;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.service.biocontrol.deptwork.IPsScehduleService;
import com.alks.function.service.biocontrol.webwork.ILastConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author MS
 * @since 2025/2/11上午9:12
 */
@RestController
@ApiRestController("/LastConfig")
@Api(value = "生产计划", tags = "成型配置")
@Slf4j
public class LastConfigController {
    @Autowired
    private ILastConfigService service;
    @Autowired
    private IPsScehduleService scehduleService;

    /*基础资料设定部分*/

    @WebLogger
    @PostMapping("/LineQuery")
    @ApiOperation(value = "基础资料-生产线查询", tags = "成型配置")
    public ResponseInfo lineQuery() {
        return ResponseInfo.ok(scehduleService.getLineQuery());
    }

    @WebLogger
    @PutMapping("/lineAdd")
    @ApiOperation(value = "基础资料-生产线新增", tags = "成型配置")
    public ResponseInfo lineAdd(@RequestBody PsLine request) {
        scehduleService.lineAdd(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PutMapping("/line/up")
    @ApiOperation(value = "基础资料-生产线修改", tags = "成型配置")
    public ResponseInfo lineUpdate(@RequestBody PsLine request,
                                @RequestParam String workShopOld) {
        scehduleService.lineUpdate(request,workShopOld);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/lineDelete")
    @ApiOperation(value = "基础资料-生产线删除", tags = "成型配置")
    public ResponseInfo lineDelete(@RequestParam@ApiParam(value = "车间代码",required = true) String workshop) {
        scehduleService.lineDelete(workshop);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/percentQuery")
    @ApiOperation(value = "基础资料-排量查询", tags = "成型配置")
    public ResponseInfo percentQuery(@RequestBody PageParam request,
                                     @RequestParam(required = false) @ApiParam("生产线名称") String line,
                                     @RequestParam(required = false) @ApiParam("日期")
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time){
        LocalDateTime time1 = time == null ? null : time.atStartOfDay();
        return scehduleService.getDayPercentQuery(request, line, time1);
    }

    @WebLogger
    @PutMapping("/percentAdd")
    @ApiOperation(value = "基础资料-排量新增", tags = "成型配置")
    public ResponseInfo percentAdd(@RequestBody PsDayPercent request) {
        scehduleService.dayPercentAdd(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PutMapping("/percent/up")
    @ApiOperation(value = "基础资料-排量更新", tags = "成型配置")
    public ResponseInfo percentUp(@RequestBody PsDayPercent request) {
        return scehduleService.dayPercentUpdate(request);
    }

    @WebLogger
    @PutMapping("/percent/add/month")
    @ApiOperation(value = "基础资料-插入月日排量资料", tags = "成型配置")
    public ResponseInfo percentMonthAdd(@RequestParam String month) {
        scehduleService.percentMonthAdd(month);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/percentDelete")
    @ApiOperation(value = "基础资料-排量删除", tags = "成型配置")
    public ResponseInfo percentDelete(@RequestParam Integer id) {
        scehduleService.dayPercentDelete(id);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/holidayQuery")
    @ApiOperation(value = "基础资料-节假日查询", tags = "成型配置")
    public ResponseInfo holidayQuery(@RequestBody PageParam request) {
        return ResponseInfo.ok(scehduleService.getHolidayQuery(request));
    }

    @WebLogger
    @PutMapping("/holidayAdd")
    @ApiOperation(value = "基础资料-节假日新增", tags = "成型配置")
    public ResponseInfo holidayAdd(@RequestBody PsHoliday request) {
        scehduleService.holidayAdd(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PutMapping("/holiday/up")
    @ApiOperation(value = "基础资料-节假日新增", tags = "成型配置")
    public ResponseInfo holidayUp(@RequestBody PsHoliday request,
                                  @RequestParam @ApiParam("旧日期")
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate holidayDateOld){
        LocalDateTime time = holidayDateOld == null ? null : holidayDateOld.atStartOfDay();
        scehduleService.holidayUpdate(request,time);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/holidayDelete")
    @ApiOperation(value = "基础资料-节假日删除", tags = "成型配置")
    public ResponseInfo holidayDelete(@RequestParam LocalDateTime holidayDateOld) {
        scehduleService.holidayDelete(holidayDateOld);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/line/back")
    @ApiOperation(value = "基础资料-线别回显", tags = "成型配置")
    public ResponseInfo lineBack(@RequestParam(required = false) @ApiParam("生产线名称") String search){
        return ResponseInfo.ok(service.lineBack(search));
    }

    /*型体产能设定*/

    @WebLogger
    @PostMapping("/ie/Query")
    @ApiOperation(value = "型体产能-型体产能查询", tags = "成型配置")
    public ResponseInfo ieQuery(@RequestBody PageParam request,
                                @RequestParam(required = false)@ApiParam("类型") String type,
                                @RequestParam(required = false)@ApiParam("品牌") String brand,
                                @RequestParam(required = false)@ApiParam("型体") String style) {
        return ResponseInfo.ok(service.ieQuery(request,type,brand,style));
    }

    @WebLogger
    @PostMapping("/ie/update")
    @ApiOperation(value = "型体产能-更新型体IE", tags = "成型配置")
    public ResponseInfo ieUpdate(@RequestBody PsIeData request) {
        service.ieUpdate(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/ie/styleUp")
    @ApiOperation(value = "型体产能-型体/品牌 更新（俩按钮）", tags = "成型配置")
    public ResponseInfo ieStyleUp(@RequestParam(required = false)@ApiParam("品牌（仅品牌更新需要）") String brand,
                                  @RequestParam@ApiParam(value = "1.按品牌更新 2.按型体更新",required = true) Integer type) {
        if (type.equals(1)&& StringUtils.isBlank(brand)){
            return ResponseInfo.error("请选择品牌");
        }
        service.ieStyleUp(type,brand);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/ie/delete")
    @ApiOperation(value = "型体产能-型体产能删除(批量)", tags = "成型配置")
    public ResponseInfo ieDelete(@RequestBody List<Integer> idList) {
        service.ieDelete(idList);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/ie/back/brand")
    @ApiOperation(value = "型体产能-品牌回显", tags = "成型配置")
    public ResponseInfo ieBrandBack(@RequestParam(required = false) @ApiParam("生产线名称") String search){
        return ResponseInfo.ok(service.ieBrandBack(search));
    }

    /*时段门槛设定*/

    @WebLogger
    @PostMapping("/time/Query")
    @ApiOperation(value = "时段门槛-时段门槛查询", tags = "成型配置")
    public ResponseInfo timeQuery(@RequestBody PageParam request,
                                @RequestParam(required = false)@ApiParam("类型") String type,
                                @RequestParam(required = false)@ApiParam("品牌") String brand,
                                @RequestParam(required = false)@ApiParam("型体") String style) {
        return ResponseInfo.ok(service.timeQuery(request,type,brand,style));
    }

    @WebLogger
    @PostMapping("/time/update")
    @ApiOperation(value = "时段门槛-时段门槛更新", tags = "成型配置")
    public ResponseInfo timeUpdate(@RequestBody PsLtData request) {
        service.timeUpdate(request);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/time/styleUp")
    @ApiOperation(value = "时段门槛-型体/品牌 更新（俩按钮）", tags = "成型配置")
    public ResponseInfo timeStyleUp(@RequestParam(required = false)@ApiParam("品牌（仅品牌更新需要）") String brand,
                                  @RequestParam@ApiParam(value = "1.按品牌更新 2.按型体更新",required = true) Integer type) {
        if (type.equals(1)&& StringUtils.isBlank(brand)){
            return ResponseInfo.error("请选择品牌");
        }
        service.timeStyleUp(type,brand);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/time/delete")
    @ApiOperation(value = "时段门槛-型体产能删除(批量)", tags = "成型配置")
    public ResponseInfo timeDelete(@RequestBody List<Integer> idList) {
        service.timeDelete(idList);
        return ResponseInfo.ok();
    }

    @WebLogger
    @GetMapping("/lt/back/brand")
    @ApiOperation(value = "时段门槛-品牌回显", tags = "成型配置")
    public ResponseInfo ltBrandBack(@RequestParam(required = false) @ApiParam("生产线名称") String search){
        return ResponseInfo.ok(service.ltBrandBack(search));
    }

}